package com.example.interactactivity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private EditText editText1;
    private  Button button1;
    private TextView text1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        editText1 = (EditText)findViewById(R.id.editText1);
        button1 = (Button) findViewById(R.id.button1);
        text1 = (TextView)findViewById(R.id.text1) ;
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //1创建Bundle对象准备数据
                Bundle bundle = new Bundle();
                bundle.putInt("num",1);//把1写入bundle标号为name
                bundle.putString("msg",text1.getText().toString());
                bundle.putString("msg",editText1.getText().toString());
                //2创建Intent对象，向Intent对象添加数据\
                Intent intent = new Intent(MainActivity.this, SecondActivity.class);//.class因为不知道跳转到SecondActivity可能还没有实例所以用class
                intent.putExtras(bundle);//把bundle的内容放入intent
                //3用startActivity（）方法启动新的Activity
                startActivityForResult(intent,1);  //得到新打开Activity关闭后返回的数据,第二个参数为请求码，可以根据业务需求自己编号
            }
        });
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==1) {
            //若当前 Activity 可能由多处返回，此处分支将更有必要
            Bundle retBundle = data.getExtras();//实例一个bundle
            String retMsg = retBundle.getString("msg1");
            text1.append(retMsg+"\n"+"小王："+editText1.getText().toString()+"\n");
            editText1.setText("");
            //editText1.setHint("收到回复：" + retMsg);
        }
    }

}
